/*	kd:
	
	This is the smol ring menu that opens that lets you select whether to bind
	your current weapon, spell or inventory.
	
	*/

struct Gc_BindMenu play {
	private Gc_Shop_UiHandling			ui_handling;
	private Gc_Shop_InvLayout			ring_layout;
	private Gc_Shop_DrawCache			cache;
	private Gcs_ButtonSprites			button_sprites;
	private Gc_NormalFades				ring_offsets;
	private FadeTick					layout_t;
	private Array <String>				button_action;
	private TextureId					sprite_cursor;
	
	private vector2						window_resolution;
	private vector2						sprite_resolution;
	private vector2						window_to_sprite;
	
	private bool						is_shown;
	
	const anim_tics = 15;
	
	bool OnBuild () {
		if(!ui_handling.Build()) {
			return false;
		}
		
		button_sprites.Cache();
		ring_layout.Reposition(
			normal_origin:			(0.5, 0.5),
			inner_rad:				0.15,
			outer_rad:				91337.69420,
			draw_rad:				0.33333333333333333333333333333333333333333,
			draw_offset:			0.05);
		return true;
	}
	
	bool Begin (
	PlayerInfo			player_info,
	Gcq_SelectableItems	selectable_weapons,
	Gcs_ItemType		type_weapon,
	Gcs_ItemType		type_spell,
	Gcs_ItemType		type_inv) {
		let po = Guncaster(player_info.mo);
		
		if(po == NULL) {
			return false;
		}
		
		// kd: Fill in the ring with something sane. You need to have at least
		// one of three things selected: A weapon, a spell or an inventory.
		// Otherwise, there's no point in opening the menu at all, since there's
		// no "current item" to bind.
		
		ring_layout.	ClearAndResize(0);
		cache.			ClearAndResize(0);
		button_action.	clear();
		
		let cur_inv			= po.invsel;
		let cur_weapon		= po.player.readyweapon;
		let cur_spell		= po.CurrentSpell();
		let button_count	= 0;
		
		// kd: Prepare what buttons will be shown and what actions they have.
		// At this level, we must track for ourselves what each button "is",
		// kinda similar to the shop, but less database-y and more, "oh, I knew
		// I added that button, so I might as well attach its action now."
		
		if(cur_inv != NULL) {
			let class_type = cur_inv.GetClass();
			
			if(type_inv.CanEquip(po, class_type)) {
				ring_layout.Expand();
				cache.PushFromItem(class_type, button_sprites);
				button_action.push("BindCurrentInv");
				button_count++;
			}
		}
		
		if(cur_weapon != NULL && selectable_weapons) {
			let class_type = cur_weapon.GetClass();
			
			if(
			type_weapon.CanEquip(po, class_type) &&
			selectable_weapons.Contains(class_type)) {
				ring_layout.Expand();
				cache.PushFromItem(class_type, button_sprites);
				button_action.push("BindCurrentWeapon");
				button_count++;
			}
		}
		
		if(cur_spell != NULL && type_spell.CanEquip(po, cur_spell)) {
			ring_layout.Expand();
			cache.PushFromItem(cur_spell, button_sprites);
			button_action.push("BindCurrentSpell");
			button_count++;
		}
		
		if(0 < button_count) {
			ring_layout.ClearPositions();
			ring_layout.Rescale();
			ring_offsets.ClearAndResize(button_count);
			layout_t.To(anim_tics);
		
			// kd: Same deal as with the shop in the same place.
			window_resolution	= (
				Screen.GetWidth(),
				Screen.GetHeight());
			
			let aspect_ratio	= 1.0 * window_resolution.x / window_resolution.y;
			sprite_resolution	= 720 * (aspect_ratio, 1);
			window_to_sprite	= (
				sprite_resolution.x / window_resolution.x,
				sprite_resolution.y / window_resolution.y);
			
			ui_handling.InitLayout(
				window_resolution,
				sprite_resolution);
			
			ring_layout.InitForCursor(window_resolution);
			
			// kd: It's a preference all right.
			// AND I SWEAR TO GOD IF THE CURSOR GETS OVERWRITTEN EVER AGAIN...
			
			sprite_cursor = Gc_MenuPreference.Cursor(player_info);
			
			is_shown = true;
			return true;
		}
		
		is_shown = false;
		return false;
	}
	
	void NextAnimState () {
		layout_t.Next();
		
		if(layout_t.Now() < 1) {
			is_shown = false;
		}
		
		ring_offsets.NextState();
		
		ring_layout.CacheOldPositions();
		ring_layout.NextFrameNoRot(0.4 + 0.6 * layout_t.RenderStandardDelta(anim_tics, 1));
	}
	
	void NextState (PlayerInfo player_info) {
		ui_handling.SetCanSelectItems(true);
		ui_handling.NextState(player_info, ring_layout);
		
		if(ui_handling.HasHoveredItem()) {
			ring_offsets.ToEnd(ui_handling.CurrentItem(), tics: 6);
		}
		
		if(ui_handling.HasDehoveredItem() || ui_handling.HasLeftRing()) {
			ring_offsets.ToStart(ui_handling.PreviousItem(), tics: 13);
		}
	}
	
	ui void NextCursor (int dx, int dy) const /* *WHISTLES* INTENSIFIES */ {
		ui_handling.mouse_input.NextState(dx, dy);
	}
	
	bool IsShown () const {
		return is_shown;
	}
	
	// kd: Make sure to only call this if the menu opened to begin with, or you
	// might get amusing behaviour.
	
	void EndState (int bind_index) {
		if(ui_handling.IsInsideRing()) {
			Gc_BindHandler.SendNetworkEvent(
				button_action [ui_handling.CurrentItem()],
				bind_index);
		}
		
		layout_t.ContinueTo(0);
	}
	
	ui void Draw (double t = 1) const {
		let alpha	= layout_t.RenderStandardDelta(anim_tics, t);
		
		ring_layout.BeginDraw(ring_offsets, t);
		ring_layout.DrawButtons(
			cache,
			button_sprites,
			alpha);
		ring_layout.DrawHighlight(
			cache,
			button_sprites,
			alpha);
		ring_layout.DrawIcons(
			cache,
			alpha);
		ring_layout.DrawOverlays(
			cache,
			button_sprites,
			alpha);
		
		let cursor_pos = ui_handling.mouse_input.DrawPos(t);
		
		cursor_pos = (
			cursor_pos.x * window_to_sprite.x,
			cursor_pos.y * window_to_sprite.y);
		
		Screen.DrawTexture(
			sprite_cursor,
			true,
			cursor_pos.x,
			cursor_pos.y,
			DTA_KEEPRATIO,			true,
			DTA_VIRTUALWIDTHF,		sprite_resolution.x,
			DTA_VIRTUALHEIGHTF,		sprite_resolution.y);
	}
}